﻿using DLog.ILog;
using DLog.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLog.Model;
using DLog.IStorageDal;

namespace DLog.Log
{
    public class Logger : ILogger
    {
        IDal Dal;
        public void Init(string target, string addition)
        {
            Dal = StorageFactory.StorageFactory.GetStorageDal(target, addition);
        }

        public bool Trace(string message)
        {
            return Log(message, (int)EnumLogLevel.Trace);
        }
        public bool Trace(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Trace);
        }
        public bool Trace(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Trace);
        }
        public bool Debug(string message)
        {
            return Log(message, (int)EnumLogLevel.Debug);
        }
        public bool Debug(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Debug);
        }
        public bool Debug(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Debug);
        }
        public bool Info(string message)
        {
            return Log(message, (int)EnumLogLevel.Info);
        }
        public bool Info(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Info);
        }
        public bool Info(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Info);
        }
        public bool Warn(string message)
        {
            return Log(message, (int)EnumLogLevel.Warn);
        }
        public bool Warn(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Warn);
        }
        public bool Warn(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Warn);
        }
        public bool Error(string message)
        {
            return Log(message, (int)EnumLogLevel.Error);
        }
        public bool Error(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Error);
        }
        public bool Error(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Error);
        }
        public bool Fatal(string message)
        {
            return Log(message, (int)EnumLogLevel.Fatal);
        }
        public bool Fatal(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Fatal);
        }
        public bool Fatal(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Fatal);
        }
        public bool Log(string meesage, int logLevel)
        {
            LogModel lm = new Model.LogModel() { 
                CreateDate=DateTime.Now,
                LogLevel=logLevel,
                Message=meesage,
                StackTrade = string.Empty,
                Origin = string.Empty
            };
            return Dal.Insert(lm)>0;
        }
        public bool Log(Exception ex, int logLevel)
        {
            LogModel lm = new Model.LogModel()
            {
                CreateDate = DateTime.Now,
                LogLevel = logLevel,
                Message = ex.Message,
                StackTrade=ex.StackTrace,
                Origin=ex.Source
            };
            return Dal.Insert(lm)>0;
        }
        public bool Log(string meesage, Exception ex, int logLevel)
        {
            LogModel lm = new Model.LogModel()
            {
                CreateDate = DateTime.Now,
                LogLevel = logLevel,
                Message = meesage+"\t\nException:"+ex.Message,
                StackTrade = ex.StackTrace,
                Origin = ex.Source
            };
            return Dal.Insert(lm)>0;
        }

        public LogModel GetLog(int LogId)
        {
            return Dal.SelectModel(LogId);
        }
        public List<LogModel> GetLogs(int logLevel, int pageIndex, int pageSize)
        {
            return Dal.SelectList(logLevel, pageIndex, pageSize);
        }
        public List<LogModel> GetLogs(DateTime CreateStartDate, DateTime CreateEndDate, int pageIndex, int pageSize)
        {
            return Dal.SelectList(CreateStartDate, CreateEndDate, pageIndex, pageSize);
        }
        public List<LogModel> GetLogs(int logLevel, DateTime CreateStartDate, DateTime CreateEndDate, int pageIndex, int pageSize)
        {
            return Dal.SelectList(logLevel, CreateStartDate, CreateEndDate, pageIndex, pageSize);
        }

        public bool DeleteModel(int LogId)
        {
            return true;
        }
        public bool DeleteList(List<int> LogId)
        {
            return true;
        }
    }
}
